package server.core.dao;

import java.util.HashMap;

import server.core.base.Airplane;
import server.core.base.Airport;
import server.core.base.EntityBase;
import server.core.base.Flight;
import server.core.base.Terrain;
import server.core.base.Ticket;
import server.core.base.User;
import server.core.dao.DaoFactory.daoType;

public class MemoryDAO implements Dao{
	private static final daoType DAO_TYPE=daoType.MEMORY;
	private HashMap<Long,Airplane> airplaneMap=new HashMap<Long,Airplane>();
	private HashMap<Long,Airport> airportMap=new HashMap<Long,Airport>();
	private HashMap<Long,User> customerMap=new HashMap<Long,User>();
	private HashMap<Long,Flight> flightMap=new HashMap<Long,Flight>();
	private HashMap<Long,Ticket> ticketMap=new HashMap<Long,Ticket>();
	private HashMap<Long,Terrain> terrainMap=new HashMap<Long,Terrain>();
	
	protected MemoryDAO(){}
	public static final MemoryDAO INSTANCE = new MemoryDAO();
	
	@Override
	public HashMap<Long,? extends EntityBase> getEntityMap(Class<? extends EntityBase> entityObject){
		if(Airplane.class.equals(entityObject))
			return airplaneMap;
		else if(Airport.class.equals(entityObject))
			return airportMap;
		else if(User.class.equals(entityObject))
			return customerMap;
		else if(Flight.class.equals(entityObject))
			return flightMap;
		else if(Ticket.class.equals(entityObject))
			return ticketMap;
		else if(Terrain.class.equals(entityObject))
			return terrainMap;
		else
		return null;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean setEntityMap(Class<? extends EntityBase> entityClass,HashMap<Long,? extends EntityBase> hashmap1){
		if(Airplane.class.equals(entityClass)){
			this.airplaneMap=(HashMap<Long, Airplane>) hashmap1;
			return true;
		}
		else if(Airport.class.equals(entityClass)){
			this.airportMap=(HashMap<Long, Airport>) hashmap1;
			return true;
		}
		else if(User.class.equals(entityClass)){
			this.customerMap=(HashMap<Long, User>) hashmap1;
			return true;
		}
		else if(Flight.class.equals(entityClass)){
			this.flightMap=(HashMap<Long, Flight>) hashmap1;
			return true;
		}
		else if(Ticket.class.equals(entityClass)){
			this.ticketMap=(HashMap<Long, Ticket>) hashmap1;
			return true;
		}
		else if(Terrain.class.equals(entityClass)){
			this.terrainMap=(HashMap<Long, Terrain>) hashmap1;
			return true;
		}
		else
		return false;
	}
	
	@Override
	public long saveObject(EntityBase entity) {
		if(Airplane.class.equals(entity.getClass())){
			airplaneMap.put(entity.getId(),(Airplane) entity);
			return entity.getId();
		}
		else if(Airport.class.equals(entity.getClass())){
			airportMap.put(entity.getId(), (Airport) entity);
			return entity.getId();
		}
		else if(User.class.equals(entity.getClass())){
			customerMap.put(entity.getId(), (User) entity);
			return entity.getId();
		}
		else if(Flight.class.equals(entity.getClass())){
			flightMap.put(entity.getId(), (Flight) entity);
			return entity.getId();
		}
		else if(Ticket.class.equals(entity.getClass())){
			ticketMap.put(entity.getId(), (Ticket) entity);
			return entity.getId();
		}
		else if(Terrain.class.equals(entity.getClass())){
			terrainMap.put(entity.getId(), (Terrain) entity);
			return entity.getId();
		}
		else return 0;
	}
	@Override
	public boolean deleteObject(Class<? extends EntityBase> entityClass,long id) {
		if(Airplane.class.equals(entityClass)){
			airplaneMap.remove(id);
			return true;
		}
		else if(Airport.class.equals(entityClass)){
			airportMap.remove(id);
			return true;
		}
		else if(User.class.equals(entityClass)){
			customerMap.remove(id);
			return true;
		}
		else if(Flight.class.equals(entityClass)){
			flightMap.remove(id);
			return true;
		}
		else if(Ticket.class.equals(entityClass)){
			ticketMap.remove(id);
			return true;
		}
		else if(Terrain.class.equals(entityClass)){
			terrainMap.remove(id);
			return true;
		}
		return false;
	}
	@Override
	public EntityBase getObject(Class<? extends EntityBase> entityClass,long id) {
		if(Airplane.class.equals(entityClass))
			return airplaneMap.get(id);
		else if(Airport.class.equals(entityClass))
			return airportMap.get(id);
		else if(User.class.equals(entityClass))
			return customerMap.get(id);
		else if(Flight.class.equals(entityClass))
			return flightMap.get(id);
		else if(Ticket.class.equals(entityClass))
			return ticketMap.get(id);
		else if(Terrain.class.equals(entityClass))
			return terrainMap.get(id);
		else
		return null;
	}

	@Override
	public daoType getDaoType() {
		return DAO_TYPE;
	}

}
